package hp.com.service.impl;

import hp.com.dao.CartDataDAO;
import hp.com.dao.OrdersDAO;
import hp.com.dao.impl.CartDataDAOImpl;
import hp.com.dao.impl.OrderDAOImpl;
import hp.com.entity.*;
import hp.com.service.AddressService;
import hp.com.service.CartDataService;
import hp.com.service.OrderService;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class OrderServiceImpl implements OrderService {
    OrdersDAO dao = new OrderDAOImpl();
    @Override
    public List<Orders> GetOrderByUserId(int id) throws Exception {
        return dao.GetOrdersByUserId(id);
    }


    public String DoInsertOrder(String cart, String addr, Users user)throws Exception{
        Orders order=new Orders();
        CartDataDAO cartDao= new CartDataDAOImpl();
        OrdersDAO orderDao = new OrderDAOImpl();
        AddressService addrService = new AddressServiceImpl();
        //通过ID获取地址明文信息
        String addrText=addrService.getAddressTextById(Integer.parseInt(addr));
        //获取购物车数据ID数组
        String[] cartId=cart.split(",");
        List<OrderDetail> details= new ArrayList<>();
        //通过数组遍历，将购物车ID数组转换成OrderDetail对象数组，并获取总价值
        double totalPrice = 0;
        for (int i = 0; i < cartId.length; i++) {
            CartData carttrans = cartDao.GetCartDataById(Integer.parseInt(cartId[i]));
            OrderDetail detail = this.TransferOrderDetailFromCart(carttrans);
            totalPrice=totalPrice+detail.getSingeCost();
            details.add(detail);
        }
        //日期格式转换
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        //Order对象写入
        order.setSerial(this.getNewOrderNumber());
        order.setUserAddress(addrText);
        order.setUserId(user.getId());
        order.setCreateTime(sdf.format(new Date()));
        order.setCost(totalPrice);
        order.setDetail(details);
        orderDao.DoInsertOrder(order);
        return order.getSerial();
    }

    @Override
    public String getNewOrderNumber() throws Exception {
        Random random = new Random();
        Date date = new Date();
        SimpleDateFormat spd = new SimpleDateFormat("yyyyMMddHHmmss");
        String datetime=spd.format(new Date());
        String laftFour = "";
        for (int i = 0; i < 5; i++) {
            laftFour=laftFour+random.nextInt(11);
        }
        return  datetime+laftFour;
    }

    public OrderDetail TransferOrderDetailFromCart(CartData cart) throws Exception{
        OrderDetail detail = new OrderDetail();
        detail.setGood(Integer.parseInt(cart.getItemId()));
        detail.setName(cart.getFullname());
        detail.setPic(cart.getPic());
        detail.setAmount(cart.getItemNumber());
        detail.setSingeCost(cart.getPrice());
        detail.setSUK(cart.getSuk());
        detail.setStat(0);
        return detail;
        }

    @Override
    public Orders getOrderBySerial(String serial) throws Exception {
        return dao.getOrderBySerial(serial);
    }
}
